<template>
  <div class="login">
    <van-nav-bar title="登录"
                 left-text="返回"
                 left-arrow
                 @click-left="$router.go(-1)" />
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input class="inp"
                 v-model="phone"
                 maxlength="11"
                 placeholder="请输入手机号码"
                 type="text">
        </div>
        <div class="form-item">
          <input class="inp"
                 maxlength="5"
                 placeholder="请输入图形验证码"
                 type="text"
                 v-model="picCode">
          <img :src="picBase64"
               v-if="picBase64"
               @click="getPicCode"
               alt="">
        </div>
        <div class="form-item">
          <input class="inp"
                 v-model="msgCode"
                 placeholder="请输入短信验证码"
                 type="text">
          <button @click="getCode()">{{totalSecond===currentSecond?'获取验证码':currentSecond+'秒后再获取'}}</button>
        </div>
      </div>

      <div class="login-btn"
           @click="login()">登录</div>
    </div>
  </div>
</template>
  
  <script>
import { getPicCode, getCode, login } from '@/api/login'
export default {
  name: 'loginIndex',
  data () {
    return {
      picCode: '', // 图形验证码
      phone: '',
      picBase64: '', // 图形验证码的base64编码
      picKey: '', // 图形验证码的key
      totalSecond: 5, // 总等待的时间秒数
      currentSecond: 5, // 当前的剩余的时间秒数
      timer: null, // 定时器
      msgCode: ''
    }
  },
  created () {
    this.getPicCode()
  },

  methods: {
    // 校验格式
    correct () {
      if (!/^1[3-9]\d{9}$/.test(this.phone)) {
        this.$toast('请输入正确的手机号')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        this.$toast('请输入正确的图形验证码')
        return false
      }
      return true
    },

    // 获得图形验证码
    async getPicCode () {
      const { data: { base64, key } } = await getPicCode()
      this.picBase64 = base64,
        this.picKey = key
      this.$toast('获得图形验证码成功')

    },

    // 获得短信验证码
    async getCode () {
      // 校验书写合法
      if (!this.correct()) {
        return
      }

      /* 
      已经放到统一拦截器处理，只要是非200的直接抛出错误

            // (已解决)有bug：无论验证码是否符合都发送成功
            // 请求验证码
            const code = await getCode(this.picCode, this.picKey, this.phone)
            console.log(code);
      
            // 校验code是否正确
            if (code.status === 500) {
              this.$toast(code.message)
              return
            } 
            
      */

      // 进入倒计时
      if (!this.timer && this.totalSecond === this.currentSecond) {

        await getCode(this.picCode, this.picKey, this.phone)

        this.timer = setInterval(() => {
          this.currentSecond--
          if (this.currentSecond <= 0) {
            clearInterval(this.timer)
            this.timer = null
            this.currentSecond = this.totalSecond
          }
        }, 1000);
        this.$toast('发送成功，请注意查收')
      }

    },

    // 登录
    async login () {
      if (!this.correct()) {
        return
      }
      if (!/^\d{6}$/.test(this.msgCode)) {
        return this.$toast('请输入正确的手机验证码')
      }
      const res = await login(this.phone, this.msgCode)
      console.log(res);
      this.$store.commit('user/setUserInfo', res.data)

      // 判断是否有查询参数
      const url = this.$route.query.backUrl || '/'
      this.$router.replace(url)
      this.$toast('登录成功')
    }
  }


}
  </script>
  
  <style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg, #ecb53c, #ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0, 0, 0, 0.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>